package com.cskaoyan.shiro;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @Author Pudding
 * @Date 2024/6/14 15:53
 */
public class CustomSessionManager extends DefaultWebSessionManager {
    @Override
    protected Serializable getSessionId(ServletRequest servletRequest, ServletResponse response) {
        //1. 先强转类型
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //2. 获取请求的sessionId
        //小程序的其他请求就会携带这个SessionId过来，而前端请求头携带sessionId的key为X-CskaoyanMarket-Token
        String sessionId = request.getHeader("X-CskaoyanMarket-Token");
        //3. 进行判断，并返回sessionId
        if (sessionId != null && !"".equals(sessionId)) {
            return sessionId;
        }

        return super.getSessionId(servletRequest, response);
    }
}
